#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@File    ：ppCar_selenium.py
@Author  ：Juff
@Date    ：2023/10/13 15:56 
'''
import os
import time
import re

import pymysql
import requests

from fontTools.ttLib import TTFont
# selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options


# 用来对于被转换数据进行更新
def num_tran(strs):
    font = TTFont("./trans.woff")
    num_list_eng = font.getGlyphOrder()[1:]

    num_dict = {
        "zero": 0, "one": 1, "two": 2, "three": 3, "four": 4,
        "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9
    }
    # 转换成阿拉伯数字
    num_list_alb = [num_dict[i] for i in num_list_eng]
    # print(num_list_alb)
    # strs = '英菲尼迪-英菲尼迪Q80L 1026款 1.0T 悦享版'
    new_str = ''
    for i in strs:
        if i.isdigit():
            real_alb = num_list_alb.index(int(i))
            new_str += str(real_alb)
            # print(real_alb, type(real_alb))
        else:
            new_str += i
    return new_str


# 将数据写入数据的代码
def write_mysql(conn, cursor, data):
    # 单条数据插入
    # print(data)
    # insert_sql = ('insert into pp_twice_car(car_id ,car_info,car_date,car_mile,car_price,car_payment) '
    #               'values("%s", "%s", "%s", %s, %s, %s)') % data[0]
    # print(insert_sql)
    #
    # cursor.execute(insert_sql)
    # conn.commit()

    try:
        insert_sql = ('insert into pp_twice_car(car_id ,car_info,car_date,car_mile,car_price,car_payment,city) '
                      'values(%s, %s, %s, %s, %s, %s, %s)')

        cursor.executemany(insert_sql, data)
    except Exception as e:
        conn.rollback()
        print(e)
    else:
        conn.commit()


# 爬虫及数据解析
def ppCarSpider(url, driver, city):
    # 请求数据
    driver.get(url)

    data = []

    # 获取woff文件的写法

    if not os.path.exists("./trans.woff"):
        url_detail = driver.find_element(By.XPATH, '//style[1]').get_attribute("innerHTML")
        print(url_detail)
        woff_url = re.match(r"[\s\S]*?url\(\'(.*\.woff)", url_detail).group(1)
        resp = requests.get(woff_url)
        with open("./trans.woff", 'wb') as f:
            f.write(resp.content)

    # 页面数据
    # driver.page_source
    li_list = driver.find_elements(By.XPATH, "//ul[@class='row-fluid list-row js-car-list']/li")
    # print(li_list)

    for li in li_list:
        try:
            car_id = li.find_element(By.XPATH, "./a").get_attribute("data-car-id")
            car_info = li.find_element(By.XPATH, "./a/h3").text
            car_mile_age = li.find_element(By.XPATH, "./a/div[@class='mileage']/span").text
            car_payment = li.find_element(By.XPATH, "./a/div[@class='tags-box']/div").text

            car_year = car_mile_age.split("/")[0].replace("年", "-").replace("月", '')
            car_mile = car_mile_age.split("/")[1].replace("万公里", "")
            car_price = car_payment.split(" ")[0].replace("万", '')
            car_payment = car_payment.split(" ")[1].replace("首付", "").replace("万", "")

            # print(
            #     car_id + "|||" + car_info + "|||" + car_year + "|||" + car_mile + "|||" + car_price + "|||" + car_payment)
        except Exception as e:
            # print(e)
            pass
        else:
            car_info = num_tran(car_info)
            car_year = num_tran(car_year)
            car_mile = num_tran(car_mile)
            # 1、查询一条数据就写入一条数据
            # 2、查询完一页数据后写入数据库
            data.append((car_id, car_info, car_year, car_mile, car_price, car_payment, city))
            # fp.write(
            #     car_id + "@" + car_info + "@" + car_year + "@" + car_mile + "@" + car_price + "@" + car_payment + "\n")
    return data


def main():
    opt = Options()
    opt.add_argument("--headless")

    # selenium驱动
    driver = webdriver.Edge(options=opt)
    # city_list = ['hf']
    city_list = ['hf', 'bj', 'sh', 'hz', 'sz', 'gz', 'nj', 'cq', 'cd', 'xa', 'cs']

    # fp = open("./ppCar.txt", "w", encoding='utf-8')

    # 创建数据库链接
    conn = pymysql.connect(host="master", user='root', password='123456', port=3306, db='ppCar')
    cursor = conn.cursor()

    for city in city_list:
        for i in range(1, 10):
            url = f"https://www.renrenche.com/{city}/ershouche/p{i}"
            data = ppCarSpider(url, driver, city)
            write_mysql(conn, cursor, data)
            time.sleep(5)

    cursor.close()
    conn.close()
    # fp.close()
    driver.close()


if __name__ == '__main__':
    main()
    pass
